#!/bin/bash

# myconfig 全剧配置
[  -f "$HOME/myconfig" ] && . $HOME/myconfig        

declare -A usage
_info () {
    echo -e "\e[32m$@\e[39m"
}
_warn () {
    echo -e "\e[93m$@\e[39m"
}
_err () {
    echo -e "\e[31m$@\e[39m"
}


desc="mi:install on centos"
#script begin

#example
#description: 
usage[idocker]="install docker"
usage[irepo]="install aliyun repo"
idocker(){
     echo -e "midocker"
}

irepo(){
     echo -e "mirepo"
}



rmsudopwd(){
    local sudoersFile=/etc/sudoers
    sudo chmod +w $sudoersFile
    echo "$(whoami) ALL=(ALL:ALL) NOPASSWD: ALL"|sudo tee -a $sudoersFile
    sudo chmod -w $sudoersFile
}

func=$1
isHelp=$2
MY_DIR=$HOME/my
usage["_r"]="show in finder"
usage["_c"]="open in vs code"
_r(){
    open $MY_DIR/script
}
_c(){
    local editor="vim"
    [ ! -z "$(which subl)" ] && editor="subl"
    [ ! -z "$(which code)" ] && editor="code"
    $editor $MY_DIR/script
}
    if [ "$func" = "-" -o -z "$func" ];then
echo -e "$(_info $desc):\n"    
echo -e "$(_info usage):\n"
for key in "${!usage[@]}";do
    echo -e "$(_info $key): \n\t${usage[$key]}"
done

else


if [ "$(type -t $func)" = "function" ];then 

    shift 
    if [ "$isHelp" = "-" ];then 
        #usage="${func}_usage"
        #echo -e "$func usage:\n ${!usage}"
        echo -e "$(_info usage:\\n$func)\n  ${usage[$func]}"
        echo "option help"
    else
       $func "$@"
    fi

fi

fi

